//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package ntd.util;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

public class AESUtils {
    private static final int AES_KEY_SIZE = 128;
    private static final String PASS_AES_SEED = "1da2ce17fccDSUNr4c9JnbdfJNm3a0f79af5c3f3";
    private static final String PASS_AES_PREFIX = "{AES}";

    public AESUtils() {
    }

    public static String encrypt(String content, String keyseed) throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
        random.setSeed(keyseed.getBytes());
        kgen.init(AES_KEY_SIZE, random);
        SecretKey secretKey = kgen.generateKey();
        byte[] enCodeFormat = secretKey.getEncoded();
        SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        byte[] byteContent = content.getBytes("utf-8");
        cipher.init(1, key);
        byte[] result = cipher.doFinal(byteContent);
        return parseByte2HexStr(result);
    }

    public static byte[] decrypt(byte[] content, String keyseed) throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        kgen.init(AES_KEY_SIZE, new SecureRandom(keyseed.getBytes()));
        SecretKey secretKey = kgen.generateKey();
        byte[] enCodeFormat = secretKey.getEncoded();
        SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(2, key);
        byte[] result = cipher.doFinal(hex2byte(content));
        return result;
    }

    public static String decryptToString(String content, String keyseed) throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
        random.setSeed(keyseed.getBytes());
        kgen.init(AES_KEY_SIZE, random);
        SecretKey secretKey = kgen.generateKey();
        byte[] enCodeFormat = secretKey.getEncoded();
        SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(2, key);
        byte[] result = cipher.doFinal(parseHexStr2Byte(content));
        return new String(result, "UTF-8");
    }

    public static String encryptForJS(String content, String keyseed) throws Exception {
        SecretKeySpec key = getKeySpecFromBytes(keyseed.toUpperCase());
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(1, key);
        byte[] byteEnc = cipher.doFinal(content.getBytes("UTF-8"));
        return parseByte2HexStr(byteEnc);
    }

    public static String decryptForJS(String content, String keyseed) throws Exception {
        SecretKeySpec key = getKeySpecFromBytes(keyseed.toUpperCase());
        Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
        cipher.init(2, key);
        String result = new String(cipher.doFinal(hex2byte(content.getBytes("UTF-8"))));
        return result.trim();
    }

    public static String parseByte2HexStr(byte[] buf) {
        StringBuffer sb = new StringBuffer();

        for (int i = 0; i < buf.length; ++i) {
            String hex = Integer.toHexString(buf[i] & 255);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }

            sb.append(hex.toUpperCase());
        }

        return sb.toString();
    }

    public static byte[] parseHexStr2Byte(String hexStr) {
        if (hexStr != null && hexStr.length() >= 1) {
            byte[] result = new byte[hexStr.length() / 2];

            for (int i = 0; i < hexStr.length() / 2; ++i) {
                int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
                int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
                result[i] = (byte) (high * 16 + low);
            }

            return result;
        } else {
            return null;
        }
    }

    public static byte[] hex2byte(byte[] b) {
        if (b.length % 2 != 0) {
            throw new IllegalArgumentException("长度不是偶数!");
        } else {
            byte[] b2 = new byte[b.length / 2];

            for (int n = 0; n < b.length; n += 2) {
                String item = new String(b, n, 2);
                b2[n / 2] = (byte) Integer.parseInt(item, 16);
            }

            return b2;
        }
    }

    public static SecretKeySpec getKeySpecFromBytes(String strBytes) throws NoSuchAlgorithmException {
        SecretKeySpec spec = new SecretKeySpec(hex2byte(strBytes.getBytes()), "AES");
        return spec;
    }

    public static String getPassEncrypt(String passWord) {
        if (passWord != null && passWord.trim().length() > 0) {
            try {
                String enPass = encrypt(passWord, PASS_AES_SEED);
                return PASS_AES_PREFIX + enPass;
            } catch (Exception var2) {
                return null;
            }
        } else {
            return passWord;
        }
    }

    public static String getPassDecrypt(String epassWord) {
        if (epassWord != null && epassWord.trim().length() > 0 && epassWord.startsWith(PASS_AES_PREFIX)) {
            try {
                epassWord = epassWord.substring(PASS_AES_PREFIX.length());
                String enPass = decryptToString(epassWord, PASS_AES_SEED);
                return enPass;
            } catch (Exception var2) {
                return epassWord;
            }
        } else {
            return epassWord;
        }
    }

    public static void main(String[] args) {
        try {
            String encrypt = AESUtils.getPassEncrypt("root");
            System.out.println(encrypt);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
